<?php

namespace App\Repositories;

use App\Models\ParamConfig;
use Cache;
class ParamConfigRepository extends Repository
{
    static $tag = 'testing.param.config';

    static $tableName = 'testing_param_config';

    public function getById($id)
    {
        $cacheKey = getCacheKey('testing.param.config.info') . $id;
        $value = $this->remember($cacheKey, function () use ($id) {
            return ParamConfig::where('id', $id)->first();
        });
        return $value;
    }



    public function getValue($key, $default = null, $deleteHtmlTag = false)
    {
        $map = $this->get($key);
        $val = $map ? $map->value : $default;
        $val = $deleteHtmlTag ? strip_tags($val) : $val;
        return $val;
    }


    public function get($key)
    {
        $cacheKey = getCacheKey('testing.cache_key.param_config.key') . $key;
        $value = $this->remember($cacheKey, function () use ($key) {
            return ParamConfig::where('key', $key)->first();
        });
        return $value;
    }


    public function getAll()
    {
        $cacheKey = getCacheKey('testing.cache_key.param_config.list');
        $maps = $this->remember($cacheKey, function () {
            return ParamConfig::all();
        });
        return $maps;
    }

    public function getByTag($tag)
    {
        $cacheKey = getCacheKey('testing.cache_key.param_config.tag') . $tag;
        $maps = $this->remember($cacheKey, function () use ($tag) {
            return ParamConfig::where('tag', $tag)->get();
        });
        return $maps;
    }

    public function count($tag = null)
    {
        $cacheKey = getCacheKey('testing.cache_key.param_config.tag_count') . $tag;
        $count = $this->remember($cacheKey, function () use ($tag) {
            if ($tag) {
                return ParamConfig::where('tag', $tag)->count();
            }
            return ParamConfig::count();
        });
        return $count;
    }

    public function saveSettings(array $inputs)
    {
        foreach ($inputs as $key => $value) {
            $map = ParamConfig::firstOrNew([
                'key' => $key,
            ]);
            $map->tag = 'settings';
            $map->value = $value;
            $map->save();
        }
        $this->clearCache();
    }

    /**
     * 减重测评题目的获取tag对应的值
     * @param $index 题目编号
     */
    public function getDWTestingValuesByTag($index){
        $tag = 'FW.resist.item.'.$index;
        return $this->getByTag($tag);
    }

    //21天无二维码版本
    public function getDWnoQrcodeValuesByTag($index){
        $tag = 'DW.noqrcode.resist.item.'.$index;
        return $this->getByTag($tag);
    }

    //21天无二维码获取腰围结果标题
    public function getDWnoQrcodeFatWaistResult(){
        $tag = 'DW.noqrcode.fat.waist.result';
        return $this->getByTag($tag);
    }

    //益步
    public function getElbooValuesByTag($index){
        $tag = 'Elboo.resist.item.'.$index;
        return $this->getByTag($tag);
    }


    //益步获取腰围结果标题
    public function getElbooFatWaistResult(){
        $tag = 'Elboo.fat.waist.result';
        return $this->getByTag($tag);
    }

    public function getChildhoodAutismNoteByTag($index){
        if($index)
        {
            $tag = 'childhood_autism_testing_item_result_sixteen';
            return $this->getByTag($tag);
        }
        return null;
    }

    public function getPressureNoteByTag($index){
        if($index)
        {
            $tag = 'pressure_testing_item_result_fifteen';
            return $this->getByTag($tag);
        }
        return null;
    }


    public function getMentalHealthTestingNoteByTag($index){
        if($index)
        {
            $tag = 'mental_health_testing_item_result_thirty-one';
            return $this->getByTag($tag);
        }
        return null;
    }

    public function getJointDexterityTestingNoteByTag($index){
        if($index)
        {
            $tag = 'joint_dexterity_testing_item_result_seven';
            return $this->getByTag($tag);
        }
        return null;
    }





    public function getInfantBehavioralNerveTestingNoteByTag($index){
        if($index)
        {
            $tag = 'infant_behavioral_nerve_testing_item_result_twenty-one';
            return $this->getByTag($tag);
        }
        return null;
    }

    public function batchInsertData($data){
        ParamConfig::batchInsertData(self::$tableName,$data);
    }



    public function cacheTime()
    {
        return config('testing.common_cache_time');
    }


    public function model()
    {
        return app(ParamConfig::class);
    }

    public function tag()
    {
        return self::$tag;
    }
}